package T00_Utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * JDBCUtils 功能封装
 */
public class JDBCUtils {

    /**
     * 获取数据库的连接
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public  static  Connection getConnection() throws Exception{
        //1.加载配置文件
        InputStream  is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
        //System.out.println(is);
        Properties pros = new Properties();
        pros.load(is);

        //2.读取配置信息
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driver");

        //3.加载驱动
        Class.forName(driverClass);

        //4.获取连接
        Connection conn = DriverManager.getConnection(url,user,password);
        return conn;
    }

    /**
     * 增删改操作
     * @param sql
     * @param args
     * @return
     * @throws Exception
     */
    public static   boolean  update(String sql,Object ...args) throws Exception{
        Connection conn=null;
        PreparedStatement ps=null;
        try {
            //1.获取数据库连接
             conn=JDBCUtils.getConnection();
            //2.预编译sql语句,返回PreparedStatement的实例
             ps=conn.prepareStatement(sql);
            //3.填充占位符
            for(int i=0;i<args.length;i++){
                ps.setObject(i+1,args[i]);//小心参数声明错误
            }
            //System.out.println();
            return  ps.executeUpdate()>0?true:false;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
          closeResource(conn,ps);
        }
       return  false;
    }

    /**
     * 通用的查询操作
     * @param sql
     * @param args
     * @return
     * @throws Exception
     */
    public static   ResultSet  query(String sql,Object ...args) throws Exception {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            //1.获取数据库连接
            conn = JDBCUtils.getConnection();
            //2.预编译sql语句,返回PreparedStatement的实例
            ps = conn.prepareStatement(sql);
            //3.填充占位符'
            if(args!=null){
                for (int i = 0; i < args.length; i++) {
                    ps.setObject(i + 1, args[i]);//小心参数声明错误
                }
            }
            //System.out.println();
            resultSet = ps.executeQuery();
            return  resultSet;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }

    /**
     * 关闭连接和Statement的操作1
     * @param conn
     * @param statement
     */
    public  static void  closeResource(Connection conn, Statement statement){
        if(statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭连接和Statement的操作2
     * @param conn
     * @param statement
     * @param resultSet
     */
    public  static void  closeResource(Connection conn, Statement statement,ResultSet resultSet){
        if(statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
